<template>
  <div class="dashboard">
    <div class="dashboard-header">
      <h2>控制面板</h2>
      <div class="dashboard-stats">
        <el-statistic title="总设备数" :value="devices.length" />
        <el-statistic title="运行中" :value="activeDevicesCount" />
        <div class="scene-info">
          <div class="scene-label">当前场景</div>
          <div class="scene-value">{{ getSceneName(currentScene) }}</div>
        </div>
      </div>
    </div>
    
    <!-- 快速控制 -->
    <div class="quick-controls">
      <h3>快速控制</h3>
      <div class="control-buttons">
        <el-button 
          type="success" 
          size="large"
          @click="turnOnAllDevices"
          :disabled="activeDevicesCount === devices.length"
        >
          <el-icon><Switch /></el-icon>
          全部开启
        </el-button>
        
        <el-button 
          type="danger" 
          size="large"
          @click="turnOffAllDevices"
          :disabled="activeDevicesCount === 0"
        >
          <el-icon><Close /></el-icon>
          全部关闭
        </el-button>
        
        <el-button 
          type="warning" 
          size="large"
          @click="setSceneMode('away')"
        >
          <el-icon><Location /></el-icon>
          离家模式
        </el-button>
      </div>
    </div>
    
    <!-- 设备概览 -->
    <div class="device-overview">
      <h3>设备概览</h3>
      
      <div v-if="devices.length === 0" class="empty-state">
        <el-empty description="暂无设备">
          <el-button type="primary" @click="navigateToDevices">
            添加设备
          </el-button>
        </el-empty>
      </div>
      
      <div v-else class="overview-grid">
        <!-- 按房间显示设备 -->
        <div v-for="(roomDevices, room) in devicesByRoom" :key="room" class="room-overview">
          <div class="room-header">
            <h4>
              <el-icon><House /></el-icon>
              {{ room }}
            </h4>
            <el-tag :type="getRoomStatusType(roomDevices) || 'info'">
              {{ getActiveCount(roomDevices) }}/{{ roomDevices.length }} 运行中
            </el-tag>
          </div>
          
          <div class="room-devices">
            <div
              v-for="device in roomDevices"
              :key="device.id"
              class="device-mini-card"
              :class="{ 'device-on': device.status === 'on' }"
              @click="toggleDevice(device.id)"
            >
              <div class="device-icon">
                <el-icon :size="20">
                  <component :is="getDeviceIcon(device.type)" />
                </el-icon>
              </div>
              <div class="device-name">{{ device.name }}</div>
              <div class="device-status">
                <el-icon v-if="device.status === 'on'" color="#13ce66">
                  <Check />
                </el-icon>
                <el-icon v-else color="#ff4949">
                  <Close />
                </el-icon>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 最近活动 -->
    <div class="recent-activity">
      <h3>最近活动</h3>
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in recentActivities"
          :key="index"
          :timestamp="activity.timestamp"
          :type="activity.type"
        >
          {{ activity.message }}
        </el-timeline-item>
      </el-timeline>
    </div>
  </div>
</template>

<script>
/* eslint-disable no-unused-vars */
import { 
  Switch, 
  Close, 
  Location, 
  House, 
  Check, 
  Lightning, 
  WindPower, 
  Grid 
} from '@element-plus/icons-vue'
/* eslint-enable no-unused-vars */
import { useDeviceStore } from '../store/deviceStore.js'
import { SCENE_CONFIGS, DEVICE_TYPES, SCENE_MODES } from '../types/device.js'

export default {
  name: 'SmartDashboard',
  props: {
    onNavigate: {
      type: Function,
      required: true
    }
  },
  setup(props) {
    const deviceStore = useDeviceStore()
    
    // 使用真实的活动数据（从 deviceStore 获取）
    
    const getSceneName = (mode) => {
      return SCENE_CONFIGS[mode]?.name || '未知模式'
    }
    
    const getDeviceIcon = (type) => {
      const iconMap = {
        [DEVICE_TYPES.LIGHT]: Lightning,
        [DEVICE_TYPES.AIR_CONDITIONER]: WindPower,
        [DEVICE_TYPES.CURTAIN]: Grid
      }
      return iconMap[type] || Grid
    }
    
    const getRoomStatusType = (devices) => {
      const activeCount = devices.filter(d => d.status === 'on').length
      if (activeCount === 0) return 'info'
      if (activeCount === devices.length) return 'success'
      return 'warning'
    }
    
    const getActiveCount = (devices) => {
      return devices.filter(d => d.status === 'on').length
    }
    
    const toggleDevice = (id) => {
      deviceStore.toggleDevice(id)
    }
    
    const turnOnAllDevices = async () => {
      console.log('Dashboard: 开始全部开启设备')
      await deviceStore.turnOnAllDevices()
      console.log('Dashboard: 设备全部开启完成，开始切换场景')
      // 全部开启后自动切换到在家模式
      await deviceStore.setSceneMode(SCENE_MODES.HOME)
      console.log('Dashboard: 场景切换完成，当前场景:', deviceStore.currentScene.value)
    }
    
    const turnOffAllDevices = async () => {
      await deviceStore.turnOffAllDevices()
    }
    
    const setSceneMode = async (mode) => {
      await deviceStore.setSceneMode(mode)
    }
    
    const navigateToDevices = () => {
      props.onNavigate('devices')
    }
    
    return {
      devices: deviceStore.devices,
      devicesByRoom: deviceStore.devicesByRoom,
      activeDevicesCount: deviceStore.activeDevicesCount,
      currentScene: deviceStore.currentScene,
      recentActivities: deviceStore.recentActivities,
      getSceneName,
      getDeviceIcon,
      getRoomStatusType,
      getActiveCount,
      toggleDevice,
      turnOnAllDevices,
      turnOffAllDevices,
      setSceneMode,
      navigateToDevices
    }
  }
}
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  flex-wrap: wrap;
  gap: 20px;
}

.dashboard-header h2 {
  margin: 0;
  color: #333;
  font-size: 28px;
  font-weight: 600;
}

.dashboard-stats {
  display: flex;
  gap: 30px;
  flex-wrap: wrap;
}

.scene-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 24px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  min-width: 120px;
}

.scene-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
  font-weight: 500;
}

.scene-value {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.quick-controls {
  margin-bottom: 40px;
}

.quick-controls h3 {
  margin-bottom: 20px;
  color: #333;
  font-size: 20px;
}

.control-buttons {
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
}

.control-buttons .el-button {
  flex: 1;
  min-width: 150px;
}

.device-overview {
  margin-bottom: 40px;
}

.device-overview h3 {
  margin-bottom: 20px;
  color: #333;
  font-size: 20px;
}

.empty-state {
  text-align: center;
  padding: 60px 0;
}

.overview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.room-overview {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.room-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.room-header h4 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  color: #333;
  font-size: 16px;
}

.room-devices {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}

.device-mini-card {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 12px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 2px solid transparent;
}

.device-mini-card:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.device-mini-card.device-on {
  background: #f0f9ff;
  border-color: #13ce66;
}

.device-icon {
  margin-bottom: 8px;
  color: #666;
}

.device-mini-card.device-on .device-icon {
  color: #13ce66;
}

.device-name {
  font-size: 12px;
  color: #333;
  margin-bottom: 5px;
  font-weight: 500;
}

.device-status {
  font-size: 14px;
}

.recent-activity h3 {
  margin-bottom: 20px;
  color: #333;
  font-size: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .dashboard-header {
    flex-direction: column;
    align-items: stretch;
  }
  
  .dashboard-stats {
    justify-content: center;
  }
  
  .control-buttons {
    flex-direction: column;
  }
  
  .control-buttons .el-button {
    min-width: auto;
  }
  
  .overview-grid {
    grid-template-columns: 1fr;
  }
  
  .room-devices {
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  }
}
</style>
